home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / redakcyjne / programy / Weather Watcher Live 6.1.23 / WWL6123.exe / {app} / Skins / Interface / Splendid2 / Includes / NiceTitle.js < prev    next >
Text File  |  2009-09-30  |  10KB  |  307 lines

  1. var supressNiceTitles = false;
  2.  
  3. function NiceTitles(sTemplate, nDelay, nStringMaxLength, nMarginX, nMarginY, sContainerID, sClassName){
  4.     var oTimer;
  5.     var isActive = false;
  6.     var showingAlready = false; //added var to start cursor tracking only AFTER delay occurs in show();
  7.     var sNameSpaceURI = "http://www.w3.org/1999/xhtml";
  8.     
  9.     if(!sTemplate){ sTemplate = "attr(nicetitle)";}
  10.     if(!nDelay || nDelay <= 0){ nDelay = false;}
  11.     if(!nStringMaxLength){ nStringMaxLength = 999999999; }
  12.     if(!nMarginX){ nMarginX = 15; }
  13.     if(!nMarginY){ nMarginY = 0; }
  14.     if(!sContainerID){ sContainerID = "nicetitlecontainer";}
  15.     if(!sClassName){ sClassName = "nicetitle";}
  16.  
  17.     var oContainer = document.getElementById(sContainerID);
  18.     if(!oContainer){
  19.         oContainer = document.createElementNS ? document.createElementNS(sNameSpaceURI, "div") : document.createElement("div");
  20.         oContainer.setAttribute("id", sContainerID);
  21.         oContainer.className = sClassName;
  22.         oContainer.style.display = "none";
  23.         
  24.         document.getElementsByTagName("body").item(0).appendChild(oContainer);
  25.     }
  26.     
  27.     //=====================================================================
  28.     // Method addElements (Public)
  29.     //=====================================================================
  30.     this.addElements = function addElements(collNodes, sAttribute){
  31.         var currentNode, sTitle;
  32.         
  33.         for(var i = 0; i < collNodes.length; i++){
  34.             currentNode = collNodes[i];
  35.         
  36.             sTitle = currentNode.getAttribute(sAttribute);
  37.             if(sTitle){
  38.                 currentNode.setAttribute("nicetitle", sTitle);
  39.                 currentNode.removeAttribute(sAttribute);
  40.                 addEvent(currentNode, 'mouseover', show);
  41.                 addEvent(currentNode, 'mouseout', hide);
  42.                 addEvent(currentNode, 'mousemove', reposition); //added to allow cursor tracking
  43.                 //addEvent(currentNode, 'focus', show);
  44.                 addEvent(currentNode, 'blur', hide);
  45.             }
  46.         }
  47.  
  48.     }
  49.     
  50.     //=====================================================================
  51.     // Other Methods (All Private)
  52.     //=====================================================================
  53.     function show(e){
  54.         if (supressNiceTitles == true)
  55.         {    //*************************************************
  56.             //*  Hack to prevent nice titles from displaying  *
  57.             //*************************************************
  58.             return;
  59.         }
  60.         
  61.         if (isActive){ hide(); }
  62.         
  63.         var oNode = window.event ? window.event.srcElement : e.currentTarget;
  64.         if(!oNode.getAttribute("nicetitle")){ 
  65.             while(oNode.parentNode){
  66.                 oNode = oNode.parentNode; // immediately goes to the parent, thus we can only have element nodes
  67.                 if(oNode.getAttribute("nicetitle")){ break;    }
  68.             }
  69.         }
  70.         
  71.         var sOutput = parseTemplate(oNode);
  72.         setContainerContent(sOutput);
  73.         
  74.         var oPosition = getPosition(e, oNode);
  75.         oContainer.style.left = oPosition.x;
  76.         oContainer.style.top = oPosition.y;
  77.  
  78.         //added showingAlready. cursor tracks only after delay occurs
  79.         if(nDelay){    
  80.             oTimer = setTimeout(function(){oContainer.style.display = "block"; showingAlready = true;}, nDelay);
  81.         } else {
  82.             oContainer.style.display = "block";
  83.         }
  84.  
  85.         isActive = true;        
  86.         // Let's put this event to a halt before it starts messing things up
  87.         window.event ? window.event.cancelBubble = true : e.stopPropagation();
  88.     }
  89.     
  90.     function hide(){
  91.         clearTimeout(oTimer);
  92.         oContainer.style.display = "none";
  93.         removeContainerContent();
  94.         isActive = false;
  95.         showingAlready = false;
  96.     }
  97.     
  98.     //function added to allow cursor tracking
  99.     function reposition(e){
  100.         if (supressNiceTitles == true)
  101.         {    //*************************************************
  102.             //*  Hack to prevent nice titles from displaying  *
  103.             //*************************************************
  104.             hide();
  105.             return;
  106.         }
  107.     
  108.         var oNode = window.event ? window.event.srcElement : e.currentTarget;
  109.  
  110.         var oPosition = getPosition(e, oNode);
  111.         oContainer.style.left = oPosition.x;
  112.         oContainer.style.top = oPosition.y;
  113.         
  114.         if(showingAlready){
  115.         oContainer.style.display = "block";}
  116.         else{
  117.         oContainer.style.display = "none";}
  118.         
  119.         isActive = true;
  120.         // Let's put this event to a halt before it starts messing things up
  121.         window.event ? window.event.cancelBubble = true : e.stopPropagation();
  122.     }
  123.  
  124.     function setContainerContent(sOutput){
  125.         sOutput = sOutput.replace(/&/g, "&");
  126.         if(document.createElementNS && window.DOMParser){
  127.             var oXMLDoc = (new DOMParser()).parseFromString("<root xmlns=\""+sNameSpaceURI+"\">"+sOutput+"</root>", "text/xml");
  128.             var oOutputNode = document.importNode(oXMLDoc.documentElement, true);
  129.             var oChild = oOutputNode.firstChild;
  130.             var nextChild;
  131.             while(oChild){
  132.                 nextChild = oChild.nextSibling; // Once the child is appended, the nextSibling reference is gone
  133.                 oContainer.appendChild(oChild);
  134.                 oChild = nextChild;
  135.             }
  136.         } else {
  137.             oContainer.innerHTML = sOutput;
  138.         }
  139.     }
  140.     
  141.     function removeContainerContent(){
  142.         var oChild = oContainer.firstChild;
  143.         var nextChild;
  144.  
  145.         if(!oChild){ return; }
  146.         while(oChild){
  147.             nextChild = oChild.nextSibling;
  148.             try {oContainer.removeChild(oChild);} catch(err){}
  149.             oChild =  nextChild;
  150.         }
  151.     }
  152.     
  153.     function getPosition(e, oNode){
  154.         var oViewport = getViewport();
  155.         var oCoords;
  156.         var commonEventInterface = window.event ? window.event : e;
  157.  
  158.         if(commonEventInterface.type == "focus"){
  159.             oCoords = getNodePosition(oNode);    
  160.             oCoords.x += nMarginX;
  161.             oCoords.y += nMarginY;            
  162.         } else {
  163.             oCoords = { x : commonEventInterface.clientX + oViewport.x + nMarginX, y : commonEventInterface.clientY + oViewport.y + nMarginY};
  164.         }
  165.         
  166.         // oContainer needs to be displayed before width and height can be retrieved
  167.         if(showingAlready == false) // if statement prevents flickering in os x firefox when tracking cursor
  168.             {oContainer.style.visiblity = "hidden"; 
  169.              oContainer.style.display =  "block";}
  170.         var containerWidth = oContainer.offsetWidth;
  171.         if (containerWidth > 230) containerWidth = 230;
  172.         
  173.         var containerHeight = oContainer.offsetHeight;
  174.         if(showingAlready == false)
  175.             {oContainer.style.display = "none"; 
  176.              oContainer.style.visiblity = "visible";}
  177.  
  178.         if(oCoords.x + containerWidth + 10 >= oViewport.width + oViewport.x)
  179.         {    oCoords.x = oCoords.x - nMarginX - containerWidth - 10;
  180.         }
  181.         if(oCoords.y + containerHeight + 10 >= oViewport.height + oViewport.y){
  182.             oCoords.y = oViewport.height + oViewport.y - containerHeight - oNode.offsetHeight - 10;
  183.         }
  184.  
  185.         oCoords.x += "px";
  186.         oCoords.y += "px";
  187.  
  188.         return oCoords;
  189.     }
  190.  
  191.     function parseTemplate(oNode){
  192.         var sAttribute, collOptionalAttributes;
  193.         var oFound = {};
  194.         var sResult = sTemplate;
  195.         
  196.         if(sResult.match(/content\(\)/)){
  197.             sResult = sResult.replace(/content\(\)/g, getContentOfNode(oNode));
  198.         }
  199.         
  200.         var collSearch = sResult.split(/attr\(/);
  201.         for(var i = 1; i < collSearch.length; i++){
  202.             sAttribute = collSearch[i].split(")")[0];
  203.             oFound[sAttribute] = oNode.getAttribute(sAttribute);
  204.             if(oFound[sAttribute] && oFound[sAttribute].length > nStringMaxLength){
  205.                 oFound[sAttribute] = oFound[sAttribute].substring(0, nStringMaxLength) + "...";
  206.             }
  207.         }
  208.         
  209.         var collOptional = sResult.split("?")
  210.         for(var i = 1; i < collOptional.length; i += 2){
  211.             collOptionalAttributes = collOptional[i].split("attr(");
  212.             for(var j = 1; j < collOptionalAttributes.length; j++){
  213.                 sAttribute = collOptionalAttributes[j].split(")")[0];
  214.  
  215.                 if(!oFound[sAttribute]){ sResult = sResult.replace(new RegExp("\\?[^\\?]*attr\\("+sAttribute+"\\)[^\\?]*\\?", "g"), "");    }
  216.             }
  217.         }
  218.         sResult = sResult.replace(/\?/g, "");
  219.         
  220.         for(sAttribute in oFound){
  221.             sResult = sResult.replace("attr\("+sAttribute+"\)", oFound[sAttribute]);
  222.         }
  223.         
  224.         sResult = sResult.replace(/<BR>/g,"</p><p class=\"titletext\">");
  225.         return sResult;
  226.     }    
  227.         
  228.     function getContentOfNode(oNode){
  229.         var sContent = "";
  230.         var oSearch = oNode.firstChild;
  231.  
  232.         while(oSearch){
  233.             if(oSearch.nodeType == 3){
  234.                 sContent += oSearch.nodeValue;
  235.             } else if(oSearch.nodeType == 1 && oSearch.hasChildNodes){
  236.                 sContent += getContentOfNode(oSearch);
  237.             }
  238.             oSearch = oSearch.nextSibling
  239.         }
  240.  
  241.         return sContent;
  242.     }
  243.     
  244.     function getNodePosition(oNode){
  245.         var x = 0;
  246.         var y = 0;
  247.  
  248.         do {
  249.             if(oNode.offsetLeft){ x += oNode.offsetLeft }
  250.             if(oNode.offsetTop){ y += oNode.offsetTop }
  251.         }    while((oNode = oNode.offsetParent) && !document.all) // IE gets the offset 'right' from the start
  252.  
  253.         return {x : x, y : y}
  254.     }
  255.     
  256.     // Idea from 13thParallel: http://13thparallel.net/?issue=2002.06&title=viewport
  257.     function getViewport(){
  258.         var width = 0;
  259.         var height = 0;
  260.         var x = 0;
  261.         var y = 0;
  262.         
  263.         if(document.documentElement && document.documentElement.clientWidth){
  264.             width = document.documentElement.clientWidth;
  265.             height = document.documentElement.clientHeight;
  266.             x = document.documentElement.scrollLeft;
  267.             y = document.documentElement.scrollTop;
  268.         } else if(document.body && document.body.clientWidth){
  269.             width = document.body.clientWidth;
  270.             height = document.body.clientHeight;
  271.             x = document.body.scrollLeft;
  272.             y = document.body.scrollTop;
  273.         }
  274.         // we don't use an else if here, since Opera 7 tends to get the height on the documentElement wrong
  275.         if(window.innerWidth){ 
  276.             width = window.innerWidth - 18;
  277.             height = window.innerHeight - 18;
  278.         }
  279.         
  280.         if(window.pageXOffset){
  281.             x = window.pageXOffset;
  282.             y = window.pageYOffset;
  283.         } else if(window.scrollX){
  284.             x = window.scrollX;
  285.             y = window.scrollY;
  286.         }
  287.         
  288.         return {width : width, height : height, x : x, y : y };        
  289.     }
  290. }
  291.  
  292. //=====================================================================
  293. // Event Listener
  294. // by Scott Andrew - http://scottandrew.com
  295. // edited by Mark Wubben, <useCapture> is now set to false
  296. //=====================================================================
  297. function addEvent(obj, evType, fn){
  298.     if(obj.addEventListener){
  299.         obj.addEventListener(evType, fn, false); 
  300.         return true;
  301.     } else if (obj.attachEvent){
  302.         var r = obj.attachEvent('on'+evType, fn);
  303.         return r;
  304.     } else {
  305.         return false;
  306.     }
  307. }